package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.Account;
import org.csu.mypetstore.domin.Address;
import org.csu.mypetstore.persistence.AddressMapper;
import org.csu.mypetstore.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("AddressService")
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    //通过username获得此人所有地址
    public CommonResponse<List<Address>> getAddressListByUsername(String username){
        QueryWrapper<Address> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<Address> addressList=addressMapper.selectList(queryWrapper);
        if(addressList==null){
            return CommonResponse.createForError("通过username查找地址失败");
        }
        return CommonResponse.createForSuccess("查找成功",addressList);
    }

    //添加地址
    public CommonResponse<Address> addAddress(Address address){
        int x=addressMapper.insert(address);
        if(x==0){
            return CommonResponse.createForError("增加用户失败");
        }
        return CommonResponse.createForSuccess("增加用户成功",address);
    }

    //更新地址
    public CommonResponse<Address> updateAddress(Address address){
        UpdateWrapper<Address> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",address.getId());
        int x=addressMapper.update(address,updateWrapper);
        if(x==0){
            return CommonResponse.createForError("修改用户失败");
        }
        return CommonResponse.createForSuccess("修改用户成功",address);
    }

    //删除地址
    public CommonResponse<String> deleteAddress(int id){
        QueryWrapper<Address> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int x=addressMapper.delete(queryWrapper);
        if(x==0){
            return CommonResponse.createForError("删除用户失败");
        }
        return CommonResponse.createForSuccess("删除用户成功");
    }
}
